package com.ujiuye.severlet;

import com.ujiuye.Service.StudentService;
import com.ujiuye.Service.impl.StudentServiceImpl;
import com.ujiuye.demo.StudentDao;
import com.ujiuye.demo.impl.StudentDaoImpl;
import com.ujiuye.entity.Student;
import com.ujiuye.util.PackageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/studentServlet")
@MultipartConfig
public class StudentServlet extends BaseServlet {
    StudentService sd = new StudentServiceImpl();

    protected void getAllS(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Student> list = sd.getAllS();
        request.setAttribute("stuList", list);
        request.getRequestDispatcher("student.jsp").forward(request, response);
    }

    protected void getStudentByPage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = request.getParameter("page");
        int rows = 3;
        int countRows = sd.getCountRows();
        PackageUtil pu = new PackageUtil(page, rows, countRows);
        List<Student> list = sd.getStudentByPage(pu);
        request.setAttribute("stuList", list);
        request.setAttribute("pu", pu);
        request.getRequestDispatcher("student.jsp").forward(request, response);
    }

    protected void addStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String sname = request.getParameter("sname");
        int sage = Integer.parseInt(request.getParameter("sage"));
        String ssex = request.getParameter("ssex");
        String semail = request.getParameter("semail");
        //处理文件对象，获取上传文件对象
        Part part = request.getPart("sphoto");
        //获取文件名，UUID.randomUUID()生成随机字符串
        String fileName = UUID.randomUUID() + part.getSubmittedFileName();
        String path = "D:/pic";
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        part.write(path + "/" + fileName);
        //将数据放到数据库
        Student stu = new Student(0, sname, sage, ssex, semail, fileName);
        boolean res = sd.addStudent(stu);
        if (res) {
            response.sendRedirect("studentServlet?flag=getStudentByPage&page=1");
        } else {
            response.sendRedirect("addStudent.jsp");
        }
    }

    protected void delStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int sid = Integer.parseInt(request.getParameter("sid"));
        boolean res = sd.delStudent(sid);
        response.sendRedirect("studentServlet?flag=getStudentByPage&page=1");
    }

    protected void getStudentById(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int sid = Integer.parseInt(request.getParameter("sid"));
        Student stu = sd.getStudentById(sid);
        request.setAttribute("stu", stu);
        request.getRequestDispatcher("editStudent.jsp").forward(request, response);
    }

    protected void editStudent(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int sid = Integer.parseInt(request.getParameter("sid"));
        String sname = request.getParameter("sname");
        int sage = Integer.parseInt(request.getParameter("sage"));
        String ssex = request.getParameter("ssex");
        String semail = request.getParameter("semail");
        Part part = request.getPart("sphoto");
        String fileName = part.getSubmittedFileName();
        if ("".equals(fileName)) {
            fileName = request.getParameter("oldPhoto");
        } else {
            fileName = UUID.randomUUID() + fileName;
            String path="D:/pic";
            File file= new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            part.write(path+"/"+fileName);
        }
        Student stu=new Student(sid,sname,sage,ssex,semail,fileName);
        boolean res = sd.editStudent(stu);
        if(res){
            response.sendRedirect("studentServlet?flag=getStudentByPage&page=1");
        }else {
            response.sendRedirect("studentServlet?flag=getStudentBySid&sid="+sid);
        }
    }
    protected void getStudentBySname(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        String sname = request.getParameter("sname");
        String page = request.getParameter("page");
        int rows =3;
        int countRows=sd.getCountRowsBySname(sname);
        PackageUtil pu = new PackageUtil(page,rows,countRows);
        List<Student> list= sd.getStudentBySname(pu,sname);
        request.setAttribute("stuList",list);
        request.setAttribute("pu",pu);
        request.setAttribute("sname",sname);
        request.getRequestDispatcher("student.jsp").forward(request,response);
    }
}
